/*
 

HACE UN COMBO DESDE LA BASE
	Recibe:
 	 idContainer - ID del div/span donde se va a dibujar el combo
	 idGenerated - ID que se le va a dar al SELECT
	 object      - Nombre del objeto que se trae de la base
	 prop		 - Propiedad que se mostrara como descripcion
	Devuelve:
		ret      - Array con los items del combo
	 
 */


function makeCombo(idContainer,idGenerated,object,prop,handler){
	var ret=new Array();
	jQuery.ajaxSetup({async:false});
	$.get("framework/load.php", {object:object,order:prop},function(data) {
		a=JSON.parse(data);
		if (!a.ok){
			alert(a.msj);
		}else{
			var str="";
			$("#" + idContainer).html("");
			if (handler!=""){
				str+="<SELECT id=" + idGenerated +" onchange='" + handler + "()' ><option value='0'>Seleccionar ..</option>";				
			}else{
				str+="<SELECT id='" + idGenerated +"'><option value='0'>Seleccionar ..</option>";
			}
			var x;
			for (x=0;x<a.rowCount;x++){
				str+="<option value='" + a.rows[x].id +"'>" + a.rows[x][prop] + "</option>";
				ret[a.rows[x].id]=a.rows[x][prop];
			}
			str+="</SELECT>";				
			$("#" + idContainer).html(str);		
		}	
					
	});		
	jQuery.ajaxSetup({async:true});
	return ret;	
}

function makeComboWithFilter(idContainer,idGenerated,object,prop,handler,filter){
	var ret=new Array();
	jQuery.ajaxSetup({async:false});
	$.get("framework/load.php", {object:object,params:JSON.stringify(filter)},function(data) {
		a=JSON.parse(data);
		if (!a.ok){
			alert(a.msj);
		}else{
			var str="";
			$("#" + idContainer).html("");
			if (handler!=""){
				str+="<SELECT id=" + idGenerated +" onchange='" + handler + "()' ><option value='0'>Seleccionar ..</option>";				
			}else{
				str+="<SELECT id='" + idGenerated +"'><option value='0'>Seleccionar ..</option>";
			}
			var x;
			for (x=0;x<a.rowCount;x++){
				str+="<option value='" + a.rows[x].id +"'>" + a.rows[x][prop] + "</option>";
				ret[a.rows[x].id]=a.rows[x][prop];
			}
			str+="</SELECT>";				
			$("#" + idContainer).html(str);		
		}	
					
	});		
	jQuery.ajaxSetup({async:true});
	return ret;	
}

function ahora(){
	return $.datepicker.formatDate('yy-mm-dd', new Date());
}

function setTable(idContainer,idSort){
	 $('#' + idContainer).dataTable({
	        "oLanguage": {
	            "sLengthMenu": "Mostrando _MENU_ registros por pagina",
	            "sZeroRecords": "No existen datos",
	            "sInfo": "Mostrando _START_ a _END_ de _TOTAL_ registros",
	            "sInfoEmpty": "No existen datos",
	            "sInfoFiltered": "(filtrado de _MAX_ registros)",
	            "sSearch": "Buscar:"
	        },
		   "sScrollY": "200px",
	        "bPaginate": false,
	        "bScrollCollapse": true,
	        "aaSorting": [[ idSort, "asc" ]]
		 });
}

function setTableSingle(idContainer,idSort,height){
	 var ret = $('#' + idContainer).dataTable({
	        "oLanguage": {
	            "sLengthMenu": "Mostrando _MENU_ registros por pagina",
	            "sZeroRecords": "No existen datos",
	            "sInfo": "Mostrando _START_ a _END_ de _TOTAL_ registros",
	            "sInfoEmpty": "No existen datos",
	            "sInfoFiltered": "(filtrado de _MAX_ registros)",
	            "sSearch": "Buscar:"
	        },
		    "sScrollY": height,
	        "bPaginate": false,
	        "aaSorting": [[ idSort, "asc" ]],
	        "bFilter": false,
	        "bInfo": false,
	        "bScrollCollapse": true
		 });
	 return ret; 
}

/*
CHECKEA SI UN OBJETO YA EXISTE CON UN DETERMINADO VALOR
	Recibe:
	 obj	      - Nombre del objeto que se trae de la base
	 field 		 - Campo que se evaluara
	 value		 - Valor del Campo
	Devuelve:
		exists   - Booleano q indica si el objeto ya esta creado
	 
*/

function checkExist(obj,field,value){
	var exists=false;
	var filter = {};
	filter[field]=value;
	jQuery.ajaxSetup({async:false});
	$.get("framework/load.php", {object:obj, params:JSON.stringify(filter) },function(data) {
		a=JSON.parse(data);
		if (!a.ok){
			alert(a.msj);
		}else{
			if (a.rowCount>0){
				exists=true;
			}
		}
			
	});	
	jQuery.ajaxSetup({async:true});
	return exists;
}


function load(obj,params){
	jQuery.ajaxSetup({async:false});
	$.get("framework/load.php", {object:obj,params:JSON.stringify(params)},
		function(data) {
				a=JSON.parse(data);
				if (!a.ok){
					alert(a.msj);
				}
	});
	jQuery.ajaxSetup({async:true});
	return a;
}

function loadById(obj,id){
	var params={};
	params["id"]=id;
	jQuery.ajaxSetup({async:false});
	$.get("framework/load.php", {object:obj,params:JSON.stringify(params)},
		function(data) {
				a=JSON.parse(data);
				if (!a.ok){
					alert(a.msj);
				}else{
					ret=a;										
				}
	});
	jQuery.ajaxSetup({async:true});
	if (a.rowCount=1){
		return a.rows[0];		
	}else{
		return null;
	}
}

function loadMap(obj,params,prop){
	var ret={};
	jQuery.ajaxSetup({async:false});
	$.get("framework/load.php", {object:obj,params:JSON.stringify(params)},
		function(data) {
				a=JSON.parse(data);
				if (!a.ok){
					alert(a.msj);
				}else{
					var x;
					for (x=0;x<a.rowCount;x++){
						ret[a.rows[x].id]=a.rows[x][prop];				
					}									
				}
	});
	jQuery.ajaxSetup({async:true});
	return ret;	
}

function loadMapOnId(obj,params,prop,propId){
	var ret={};
	jQuery.ajaxSetup({async:false});
	$.get("framework/load.php", {object:obj,params:JSON.stringify(params)},
		function(data) {
				a=JSON.parse(data);
				if (!a.ok){
					alert(a.msj);
				}else{
					var x;
					for (x=0;x<a.rowCount;x++){
						ret[a.rows[x][propId]]=a.rows[x][prop];						
					}									
				}
	});
	jQuery.ajaxSetup({async:true});
	return ret;	
}

function count(obj,params){
	var items;
	jQuery.ajaxSetup({async:false});
	$.get("framework/load.php", {object:obj,params:JSON.stringify(params)},
		function(data) {
				a=JSON.parse(data);
				if (!a.ok){
					alert(a.msj);
				}else{
					items=a;										
				}
	});
	jQuery.ajaxSetup({async:true});
	return items.rowCount;
}

function save(obj,params){
	var retID;
	jQuery.ajaxSetup({async:false});
	$.get("framework/save.php", {object:obj,params:JSON.stringify(params)},
		function(data) {
				a=JSON.parse(data);
				if (!a.ok){
					alert(a.msj);
				}else{
					retID=a.id;						
				}
		});
	jQuery.ajaxSetup({async:true});
	return retID;
}
function updateById(obj,params,id){
	jQuery.ajaxSetup({async:false});
	$.get("framework/update.php", {object:obj,params:JSON.stringify(params),id:id},
		function(data) {
				a=JSON.parse(data);
				if (!a.ok){
					alert(a.msj);
				}
		});
	jQuery.ajaxSetup({async:true});
}

function updateByFields(obj,params,fields){
	jQuery.ajaxSetup({async:false});
	$.get("framework/update.php", {object:obj,params:JSON.stringify(params),fields:JSON.stringify(fields)},
		function(data) {
				a=JSON.parse(data);
				if (!a.ok){
					alert(a.msj);
				}
		});
	jQuery.ajaxSetup({async:true});
}

function erase(obj,id){
	jQuery.ajaxSetup({async:false});
	$.get("framework/delete.php", {object:obj,id:id},
		function(data) {
				a=JSON.parse(data);
				if (!a.ok){
					alert(a.msj);										
				}
		});
	jQuery.ajaxSetup({async:true});
}

function confirmaBorra(pregunta,handlerSi,id){
	$("#preguntaSTR").html(pregunta);
	$("#btnConfirmaPregunta").attr("onclick",handlerSi + "(" + id + ")");
	$.blockUI({ 
         message: $('#preguntaModalDiv'),
         overlayCSS: {cursor:'auto'},	
     	   css: {cursor:'auto',
         	     width:'500px',
         	     height:'auto',
         	     top:'20%',
         	     lea:'30%',
         	     background:'#DCE5EA'}	
     }); 

}

function emailValido(email){
	var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return filter.test(email);
}

function listaCotas(){
	return ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];	
}
function listaIDMatrices(){
	return [2,"2"];	
}

function checkMatriz(tipo){
	return $.inArray(tipo, listaIDMatrices())!=-1;
}
function bloqueaLetras(name){
	$('#' + name).live('keydown', function (event) { 	 
		if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105) && (event.keyCode != 8) && (event.keyCode != 109)  && (event.keyCode != 9) && (event.keyCode != 37) && (event.keyCode != 39) && (event.keyCode != 46) && (event.keyCode != 188) && (event.keyCode != 190)) { 
			event.preventDefault(); 
		} 
	}); 	
}
function bloqueaLetrasNew(name){
	$('#' + name).on('keydown', function (event) { 	 
		if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105) && (event.keyCode != 8) && (event.keyCode != 109)  && (event.keyCode != 9) && (event.keyCode != 37) && (event.keyCode != 39) && (event.keyCode != 46) && (event.keyCode != 188) && (event.keyCode != 190)) { 
			event.preventDefault(); 
		} 
	}); 	
}
/*
 * Convierte una fecha en formato de MySql a Date de JS
 */
function convertToJS(date){
	var a = date.split(/[- :]/);
	var ret = new Date(a[0], a[1]-1, a[2], a[3], a[4], a[5]);
	return ret;
}
